package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.api.domain.vo.CategoryNestedVo;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author Haoran Sun
 * @since 2024-07-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public void getParentCategory(Long categoryId, LinkedList<Long> categoryIdList) {
        Category parentCategory = categoryMapper.selectOne(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getId, categoryId)
                        .select(Category::getParentId, Category::getId));
        categoryIdList.addFirst(parentCategory.getId());
        // 递归的终止条件
        if (parentCategory.getParentId() != 0) {
            getParentCategory(parentCategory.getParentId(), categoryIdList);
        }
    }

    @Override
    public List<Category> getCategoryListByParentId(Long parentId) {
        //根据父类id获取子类的分类集合
        List<Category> categories = categoryMapper.selectList(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId, parentId));
        categories.forEach(category -> {
            category.setHasChildren(categoryMapper.selectCount(
                    new LambdaQueryWrapper<Category>()
                            .eq(Category::getParentId, category.getId())
            ) > 0);
            //category.setChildren(getCategoryListByParentId(category.getId()));
        });
        return categories;
    }

    @Override
    public List<CategoryVo> getOneCategory() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0)).stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
    }

    @Override
    public List<CategoryNestedVo> tree() {
        List<Category> list = this.list();
        List<CategoryNestedVo> categoryNestedVos = list.stream().map(category -> {
            CategoryNestedVo categoryNestedVo = new CategoryNestedVo();
            BeanUtils.copyProperties(category, categoryNestedVo);
            return categoryNestedVo;
        }).toList();
        return categoryNestedVos.stream()
                .filter(categoryNestedVo -> categoryNestedVo.getParentId() == 0)
                .map(categoryNestedVo -> addChildren(categoryNestedVo,categoryNestedVos))
                .toList();
    }


    private static CategoryNestedVo addChildren(CategoryNestedVo categoryNestedVo, List<CategoryNestedVo> categoryNestedVos) {
        categoryNestedVo.setChildren(categoryNestedVos
                .stream()
                .filter(categoryNestedVo1 -> categoryNestedVo1.getParentId().equals(categoryNestedVo.getId()))
                .toList());
        //终止条件
        if (categoryNestedVo.getChildren().isEmpty()) {
            return categoryNestedVo;
        }
        //递归
        categoryNestedVo.getChildren()
                .forEach(categoryNestedVo1 -> addChildren(categoryNestedVo1, categoryNestedVos));
        return categoryNestedVo;
    }
}
